﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VinoViewModel.cs" company="Simon Carpentier">
//   Microsoft Reciprocal License (Ms-RL)
// </copyright>
// <summary>
//   Defines the VinoViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace VinoCellar.ViewModel
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;

    using VinoCellar.Model;

    public class VinoViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private VinoDataContext vinoDB;

        public VinoViewModel()
        {
            
        }

        // Class constructor, create the data context object.
        public VinoViewModel(string vinoDBConnectionString)
        {
            this.vinoDB = new VinoDataContext(vinoDBConnectionString);
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            this.vinoDB.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private ObservableCollection<WineItem> allWineItems;
        public ObservableCollection<WineItem> AllWineItems
        {
            get
            {
                return this.allWineItems;
            }
            set
            {
                this.allWineItems = value;
                this.NotifyPropertyChanged("AllWineItems");
            }
        }

        private ObservableCollection<TasteItem> allTasteItems;
        public ObservableCollection<TasteItem> AllTasteItems
        {
            get
            {
                return this.allTasteItems;
            }
            set
            {
                this.allTasteItems = value;
                this.NotifyPropertyChanged("AllTasteItems");
            }
        }

        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {
            var wineItemsInDB = from WineItem wine in vinoDB.WineItems select wine;
            AllWineItems = new ObservableCollection<WineItem>(wineItemsInDB);

            var tasteItemsInDB = from TasteItem taste in vinoDB.TasteItems select taste;
            AllTasteItems = new ObservableCollection<TasteItem>(tasteItemsInDB);

        }

        // Add a to-do item to the database and collections.
        public void AddWineItem(WineItem newWineItem)
        {
            // Add a to-do item to the data context.
            vinoDB.WineItems.InsertOnSubmit(newWineItem);

            // Save changes to the database.
            vinoDB.SubmitChanges();

            // Add a to-do item to the "all" observable collection.
            AllWineItems.Add(newWineItem);
        }

        // Remove a to-do task item from the database and collections.
        public void DeleteWineItem(WineItem wineForDelete)
        {
            // Delete associated taste item
            

            // Remove the to-do item from the "all" observable collection.
            AllWineItems.Remove(wineForDelete);

            // Remove the to-do item from the data context.
            vinoDB.WineItems.DeleteOnSubmit(wineForDelete);

            // Save changes to the database.
            vinoDB.SubmitChanges();
        }

        public void AddTasteItem(TasteItem newTasteItem)
        {
            vinoDB.TasteItems.InsertOnSubmit(newTasteItem);
            vinoDB.SubmitChanges();
            AllTasteItems.Add(newTasteItem);
        }
    }
}